const program = require('commander');
const {version} = require('./constants');
const path = require('path');

// 指令配置
const mapActions = {
    create: { // 创建
        alias: 'c', // 别名
        description: 'create a project', // 描述
        examples: [
            'zhu-cli create <project-name>'
        ]
    },
    config: { // 配置
        alias: 'conf',
        description: 'config project variable',
        examples: [
            'zhu-cli config set <k> <v>',
            'zhu-cli config get <k>'
        ]
    },
    '*': { // 其余命令
        alias: '',
        description: 'command not found',
        examples: []
    }
};
Reflect.ownKeys(mapActions).forEach(action => {
    const actionV = mapActions[action];
    program.command(action)
        .alias(actionV.alias)
        .description(actionV.description)
        .action(() => {
            if(action === '*') {
                console.log(actionV.description);
            } else {
                console.log(action);
                require(path.resolve(__dirname, action))(...process.argv.slice(3));
            }
        })
});
program.on('--help', () => {
    console.log('\r\nExamples:');
    Reflect.ownKeys(mapActions).forEach(action => {
        mapActions[action].examples.forEach(example=>{
            console.log('   '+example)
        })
    });
})
program.version(version).parse(process.argv);